﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using MySql.Data.MySqlClient;

namespace Controller
{
    partial class Form1 : Form
    {
        static Notification control;
        List<CurrentData> currentDataList; //for test purposes
        //Threading Vars
        private bool _shouldStop;

        public Form1()
        {
            InitializeComponent();
            control = new Notification();
            currentDataList = control.getCurrentDataList(); // for test purposes
            _shouldStop = false;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = Convert.ToString(currentDataList.ElementAt(0).GetAirTemp());
            textBox2.Text = Convert.ToString(currentDataList.ElementAt(0).GetSoilMoisture());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //b.MonitorCommands(); test button
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //b.MonitorCommands(); test button
            RequestStop();
            button4.Enabled = true;
            toolStripMenuItem1.Enabled = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {


            // Create the thread object. This does not start the thread.
            Thread workerThread = new Thread(this.DoWork);

            // Start the worker thread.
            workerThread.Start();
            Console.WriteLine("main thread: Starting worker thread...");

            // Loop until worker thread activates.
            while (!workerThread.IsAlive) ;

            // allow the worker thread to do some work:
            Thread.Sleep(10);

            // Request that the worker thread stop itself:
            //workerObject.RequestStop();

            // Use the Join method to block the current thread 
            // until the object's thread terminates.
            //workerThread.Join();
            Console.WriteLine("main thread: Worker thread has terminated.");
            button4.Enabled = false;
            toolStripMenuItem1.Enabled = false;

        }//button4



        //Thread NightMare!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        public void UpdateTextBox1(string text)
        {
            textBox1.Text = text;
        }
        public delegate void UpdateTextBox1Callback(string text);

        public void UpdateTextBox2(string text)
        {
            textBox2.Text = text;
        }
        public delegate void UpdateTextBox2Callback(string text);

        public void UpdateTextBox3(string text)
        {
           // textBox3.Text = text;
        }
        public delegate void UpdateTextBox3Callback(string text);

        public void DoWork()
        {
            while (_shouldStop == false)
            {
                textBox1.Invoke(new UpdateTextBox1Callback(this.UpdateTextBox1), new object[] { Convert.ToString(currentDataList.ElementAt(0).GetAirTemp()) });
                textBox1.Invoke(new UpdateTextBox2Callback(this.UpdateTextBox2), new object[] { Convert.ToString(currentDataList.ElementAt(0).GetSoilMoisture()) });
              //  textBox1.Invoke(new UpdateTextBox3Callback(this.UpdateTextBox3), new object[] { Convert.ToString(currentDataList.ElementAt(0).GetWaterStatus()) });
                Console.WriteLine("working");
                control.getThresholds();
                control.Compare();
            }
            Console.WriteLine("worker thread: terminating gracefully.");
        }
        public void RequestStop()
        {
            _shouldStop = true;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.ShowInTaskbar = false;
            }

        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;

        }

        private void Menu_Opening(object sender, CancelEventArgs e)
        {

        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            // Create the thread object. This does not start the thread.
            Thread workerThread = new Thread(this.DoWork);

            // Start the worker thread.
            workerThread.Start();
            Console.WriteLine("main thread: Starting worker thread...");

            // Loop until worker thread activates.
            while (!workerThread.IsAlive) ;

            // allow the worker thread to do some work:
            Thread.Sleep(10);

            // Request that the worker thread stop itself:
            //workerObject.RequestStop();

            // Use the Join method to block the current thread 
            // until the object's thread terminates.
            //workerThread.Join();
            Console.WriteLine("main thread: Worker thread has terminated.");
            button4.Enabled = false;
            toolStripMenuItem1.Enabled = false;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            RequestStop();
            button4.Enabled = true;
            toolStripMenuItem1.Enabled = true;
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
